const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgArr: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            slideButtons: [
                {
                    type: "warn",
                    text: "删除",
                    extClass: "test",
                    src: "/images/icon_del.svg", // icon的路径
                },
            ],
        });
        if (app.globalData.isLogin == false) {
            wx.showModal({
                title: "未登录",
                content: "您还没有登录哦，快去登录吧！",
                confirmText: "去登录",
                showCancel: false,
                success: (res) => {
                    if (res.confirm) {
                        wx.switchTab({
                            url: "/pages/myCenter/myCenter",
                        });
                    }
                },
            });
        } else {
            // 显示设计作品函数
            this.showMyDesign();
        }
    },
    slideButtonTap: function (e) {
        this.deleteImg(e);
    },
    showMyDesign: function () {
        const token = wx.getStorageSync("token");
        wx.request({
            url: `${app.globalData.url}/design`,
            method: "GET",
            header: {
                Authorization: token,
            },
            success: (res) => {
                this.setData({
                    imgArr: res.data,
                });
                this.jointPath(this.data.imgArr);
                console.log(this.data.imgArr);
            },
            fail: (err) => {
                console.log(err);
            },
        });
    },
    // 对图片进行排序
    jointPath: async function (arr) {
        var imgNameArr = [];
        for (var i = 0; i < arr.length; i++) {
            var paramArr = arr[i].designContent.split("/");
            var designId = arr[i].designId;

            var imgObj = {
                designId: arr[i].designId,
                imgName: paramArr[3],
            };
            imgNameArr.push(imgObj);
        }

        //对作品进行排序
        for (var i = 0; i < imgNameArr.length - 1; i++) {
            for (var j = 0; j < imgNameArr.length - 1 - i; j++) {
                if (imgNameArr[j].designId < imgNameArr[j + 1].designId) {
                    var temp = imgNameArr[j];
                    imgNameArr[j] = imgNameArr[j + 1];
                    imgNameArr[j + 1] = temp;
                }
            }
        }
        console.log(imgNameArr);
        this.setData({
            imgNameArr: imgNameArr,
            designId: designId,
        });
    },
    deleteImg: function (e) {
        const token = wx.getStorageSync("token");
        wx.request({
            url: `${app.globalData.url}/design/${e.target.id}`,
            method: "DELETE",
            header: {
                Authorization: token,
            },
            success: (res) => {
                this.onLoad();
            },
            fail: (err) => {
                console.log(err);
            },
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
});
